from PyQt5.QtCore import QSettings, QByteArray
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QVBoxLayout, QWidget


class Window(QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        btn=QPushButton(self)
        btn.setText("Demo")

        layout=QVBoxLayout()
        layout.addWidget(btn)
        self.setLayout(layout)

        settings = QSettings("My Compy", "My App")
        geometry = settings.value("Window/Geometry")
        print(geometry)
        if geometry:
            self.restoreGeometry(QByteArray(geometry))

        state = settings.value("State")
        print(state)
        if state:
            self.restoreState(QByteArray(state))

    def closeEvent(self, event):
            settings = QSettings("My Compy", "My App")
            settings.setValue("Window/Geometry", self.saveGeometry())
            settings.setValue("State", self.saveState())

if __name__ == '__main__':
    import sys
    app=QApplication(sys.argv)
    form=Window()
    form.show()
    sys.exit(app.exec())

